package com.cheng.imagerelated.preview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.cheng.imagerelated.photoselecview.utils.PermissionsUtils;

import java.util.ArrayList;

public class PreViewSelector {


    /**
     * 默认的requesrCode
     */
    public static final int DEFAULT_REQUEST_CODE = 999;


    public static final int RESULT_CODE = 1000;

    /**
     * 图片集合
     */
    public static final String EXTRA__IMAGES = "selected_images";

    /**
     * toolBar的颜色值
     */
    public static final String EXTRA_TOOLBARCOLOR = "toolBarColor";

    /**
     * bottomBar的颜色值
     */
    public static final String EXTRA_BOTTOMBARCOLOR = "bottomBarColor";

    /**
     * 状态栏的颜色值
     */
    public static final String EXTRA_STATUSBARCOLOR = "statusBarColor";
    /**
     * 状态栏的颜色值
     */
    public static final String EXTRA_STATUSBARTEXTCOLOR = "statusBarTextColor";

    /**
     * 初始位置
     */
    public static final String EXTRA_POSITION = "position";

    /**
     * true是点击预览按钮进入到的预览界面
     * false是点击item进入到的预览界面
     */
    public static final String EXTRA_ISPREVIEW = "isPreview";


    public static final String IS_CONFIRM = "is_confirm";


    public static PhotoSelectorBuilder builder() {
        return new PhotoSelectorBuilder();
    }

    public static class PhotoSelectorBuilder {
        private Bundle mPickerOptionsBundle;
        private Intent mPickerIntent;

        PhotoSelectorBuilder() {
            mPickerOptionsBundle = new Bundle();
            mPickerIntent = new Intent();
        }

        /**
         * Send the Intent from an Activity with a custom request code
         *
         * @param activity    Activity to receive result
         * @param requestCode requestCode for result
         */
        public void start(@NonNull Activity activity, int requestCode) {
            if (PermissionsUtils.checkReadStoragePermission(activity)) {
                activity.startActivityForResult(getIntent(activity), requestCode);
            }
        }

        /**
         * Get Intent to start {@link RvPreviewActivity}
         *
         * @return Intent for {@link RvPreviewActivity}
         */
        private Intent getIntent(@NonNull Context context) {
            mPickerIntent.setClass(context, RvPreviewActivity.class);
            mPickerIntent.putExtras(mPickerOptionsBundle);
            return mPickerIntent;
        }

        /**
         * @param activity Activity to receive result
         */
        public void start(@NonNull Activity activity) {
            start(activity, DEFAULT_REQUEST_CODE);
        }

        /**
         * 预览图片集合
         *
         * @param selected 已经选择的照片集合
         * @return PhotoSelectorBuilder
         */
        public PhotoSelectorBuilder setSelected(ArrayList<String> selected) {
            mPickerOptionsBundle.putStringArrayList(EXTRA__IMAGES, selected);
            return this;
        }

        /**
         * toolBar的颜色
         *
         * @param toolBarColor toolBar的颜色
         * @return PhotoSelectorBuilder
         */
        public PhotoSelectorBuilder setToolBarColor(@ColorInt int toolBarColor) {
            mPickerOptionsBundle.putInt(EXTRA_TOOLBARCOLOR, toolBarColor);
            return this;
        }

        /**
         * bottomBar的颜色
         *
         * @param bottomBarColor bottomBar的颜色
         * @return PhotoSelectorBuilder
         */
        public PhotoSelectorBuilder setBottomBarColor(@ColorInt int bottomBarColor) {
            mPickerOptionsBundle.putInt(EXTRA_BOTTOMBARCOLOR, bottomBarColor);
            return this;
        }

        /**
         * 状态栏的颜色
         *
         * @param statusBarColor 状态栏的颜色
         * @return PhotoSelectorBuilder
         */
        public PhotoSelectorBuilder setStatusBarColor(@ColorInt int statusBarColor) {
            mPickerOptionsBundle.putInt(EXTRA_STATUSBARCOLOR, statusBarColor);
            return this;
        }

        /**
         * 状态栏字体颜色
         */
        public PhotoSelectorBuilder setStatusTextBarColor(boolean isBack) {
            mPickerOptionsBundle.putBoolean(EXTRA_STATUSBARTEXTCOLOR, isBack);
            return this;
        }
    }
}
